package com.dkcrow.spring.transaction.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import com.dkcrow.spring.anno.MyAnnotation;
import com.dkcrow.spring.anno.MyLocalAnnotation;
import com.dkcrow.spring.transaction.dao.UserDaoImpl;

@Service
public class UserServiceImpl{
	
	final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired UserDaoImpl userDaoImpl;

	@MyAnnotation
	public void excute(){
		for(int i=0;i<10;i++){
			String id = "id"+i;
			String name="홀길동"+i;
			int age=i;
			userDaoImpl.insertUser(id,name,age);
			if(i==5){
				logger.info("현재 트랜잭션 use : {} \t name : {}",TransactionSynchronizationManager.isActualTransactionActive(),TransactionSynchronizationManager.getCurrentTransactionName());
				throw new RuntimeException("5번째 에러 발생!!!");
			}
		}
	}
	
	@MyLocalAnnotation
	private void excuteLocal(){
		for(int i=0;i<10;i++){
			String id = "id"+i;
			String name="홀길동"+i;
			int age=i;
			userDaoImpl.insertLocalUser(id,name,age);
			if(i==5){
				logger.info("현재 트랜잭션 use : {} \t name : {}",TransactionSynchronizationManager.isActualTransactionActive(),TransactionSynchronizationManager.getCurrentTransactionName());
				throw new RuntimeException("5번째 에러 발생!!!");
			}
		}
	}
	
	@MyAnnotation
	@MyLocalAnnotation
	public void excuteDup(){
		for(int i=0;i<10;i++){
			String id = "id"+i;
			String name="홀길동"+i;
			int age=i;
			userDaoImpl.insertLocalUser(id,name,age);
			if(i==5){
				logger.info("현재 트랜잭션 use : {} \t name : {}",TransactionSynchronizationManager.isActualTransactionActive(),TransactionSynchronizationManager.getCurrentTransactionName());
				throw new RuntimeException("5번째 에러 발생!!!");
			}
		}
	}
	
	public void deleteAllUser(){
		userDaoImpl.deleteAllUser();
	}
	
	public void testLocalMethod(){
		excuteLocal();
	}
}
